package cn.liujianbo.springsecurity.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基于 {@link ObjectMapper} 对象的 json 工具类
 * @author 刘建波
 * @date 2022-04-22 14:23
 */
public class JsonUtil {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        //忽略属性为null
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //忽略多余属性
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * 对象转json串
     * @param object 待转对象
     * @return json串
     */
    public static String toJsonString(Object object) {
        try {
            return OBJECT_MAPPER.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json转对象
     * @param json 待转json字符串
     * @param clazz 目标 Class类对象
     * @param <T> clazz对象对应的类型
     * @return json转换后的clazz类型对象
     */
    public static <T> T parse(String json, Class<T> clazz) {
        try {
            return OBJECT_MAPPER.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json 转对象集合
     * @param json 待转json字符串
     * @param clazz 目标 Class类对象
     * @param <T> clazz对象对应的类型
     * @return json转换后的clazz类型对象集合
     */
    public static <T> List<T> parseToList(String json, Class<T> clazz) {
        CollectionType listType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
        List<T> list = null;
        try {
            list = OBJECT_MAPPER.readValue(json, listType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * json 转Map
     * @param json 待转json字符串
     * @return json转换后的clazz类型对象集合
     */
    public static Map<String, Object> parseToMap(String json) {

        MapType mapType = OBJECT_MAPPER.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
        Map<String, Object> map = null;
        try {
            map = OBJECT_MAPPER.readValue(json, mapType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * json 转 List map
     * @param json 待转json字符串
     * @return json转换后的clazz类型对象集合
     */
    public static List<Map<String, Object>> parseToListMap(String json) {
        CollectionType collectionType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, Map.class);

        List<Map<String, Object>> list = null;
        try {
            list = OBJECT_MAPPER.readValue(json, collectionType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return list;
    }

}
